{*SMARTY*}
{add_layout file='add_adodb_admin_layout'}
{block name='main'}
   <h1>ADD ADOdb Browser</h1>
   <div itemprop="breadcrumb">
      <ul>
         <li><a href="{add::config()->base_url}?mode=show_databases" data-ajax-target="div.result.table">Server Databases</a></li>
         {if adodb_user::current_logged_in()->database}
            <li><a href="{add::config()->base_url}?mode=show_tables" data-ajax-target="div.result.table">Database: {adodb_user::current_logged_in()->database}</a></li>
         {/if}
      </ul>
   </div>
   <form method='get' class='query ajax' data-ajax-target="div.result.table">
      <h2>Query</h2>
      <textarea name='sql'>{$sql}</textarea>
      <label><input name='mode' value='query' type='radio' {if $mode != 'export_csv'}checked{/if} />Query</label>
      <label><input name='mode' value='export_csv' type='radio' {if $mode == 'export_csv'}checked{/if} />Download (CSV)</label>
      {add_include file='submit_button' text='Execute'}
   </form>
   <div class="result table">
         {if $error_message}
         <div class='error_message'>
            {$error_message}
         </div>
      {/if}
   {if $rows}
      <table>
         <caption>
         {if $mode == 'show_databases'}
            Server Databases
         {elseif $mode == 'show_tables'}
            Database Tables
         {else}
            Query Result
         {/if}
         </caption>
         <thead>
            <tr>
               {if $row_actions}
               <th colspan="{count($row_actions)}"></th>
               {/if}
               {foreach $fields as $field}
                  <th>{$field}</th>
               {/foreach}
            </tr>
         </thead>
         <tbody style="overflow:scroll">
            {foreach $rows as $row}
               <tr>
                  {foreach $row_actions as $row_action}
                     <td><a href="{$row_action->href($row)}">{$row_action->inner_html}</a></td>
                  {/foreach}
                  {foreach $fields as $field}
                     <td>{$row[$field]|escape}</td>
                  {/foreach}
               </tr>
            {/foreach}
         </tbody>
         <tfoot>
            <tr>
               <td colspan=999>{$pagination}</td>
            </tr>
         </tfoot>
      </table>
   {/if}
   </div>
{/block}
{block name=post_body_scripts}
   <script>
      $('form.query').submit(
            function(e) {
               var $this = $(this);
               var query = $this.find('textarea[name=sql]').val();
               if (!query.match(/WHERE|LIMIT/)) {
                  $this.addClass('ajax-cancelled');
                  if (confirm("Detected as slow query: are you sure you want to send this query?")) {
                     return ajax_submit_form($this);
                  }
                  else {
                     return false;
                  }
               }
            }
         );
   </script>
   {$smarty.block.parent}
   <script src="{add::config()->js_path}php.strings.min.js"></script>
   <script>
      if (location.hash) {
         var query_string = {}
         parse_str(location.search,query_string);
         for (x in query_string) {
            $('form.query').find("*[name='"+x+"']").val(query_string[x]);
         }
         $('form.query').submit();
      }
   </script>
{/block}